package com.example.smartcity4.ui.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.smartcity4.MainActivity;
import com.example.smartcity4.R;
import com.example.smartcity4.javaBean.GsonBean;
import com.example.smartcity4.javaBean.PasswordBean;
import com.example.smartcity4.reteofi.SmartService;
import com.google.gson.Gson;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class PasswordActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText newPassword;
    private EditText oldPassword;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        initView();
    }

    private void initView() {
        newPassword = (EditText) findViewById(R.id.newPassword);
        oldPassword = (EditText) findViewById(R.id.oldPassword);
        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String newPasswordString = newPassword.getText().toString().trim();
        if (TextUtils.isEmpty(newPasswordString)) {
            Toast.makeText(this, "请输入旧密码", Toast.LENGTH_SHORT).show();
            return;
        }

        String oldPasswordString = oldPassword.getText().toString().trim();
        if (TextUtils.isEmpty(oldPasswordString)) {
            Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something
        Retrofit retrofit = new Retrofit.Builder().baseUrl(MainActivity.host)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        SmartService smartService = retrofit.create(SmartService.class);
        PasswordBean bean= new PasswordBean(newPasswordString,oldPasswordString);
        Gson gson=new Gson();
        String obj=gson.toJson(bean);
        RequestBody body =RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),obj);
        Call<GsonBean>call=smartService.put_password(body,MainActivity.token);
        call.enqueue(new Callback<GsonBean>() {
                    @Override
                    public void onResponse(Call<GsonBean> call, Response<GsonBean> response) {
                        if (Integer.valueOf(response.body().code)==200){
                            Toast.makeText(getApplicationContext(),"修改成功", Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(getApplicationContext(),"修改失败", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onFailure(Call<GsonBean> call, Throwable throwable) {

                    }
                });

    }
}